
package com.hazelcast.internal.config.override;

import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Validation mechanism used to make sure
 * that external configuration (coming from env/system properties) features is valid.
 */
final class ConfigOverrideValidator {

    private static final ILogger LOGGER = Logger.getLogger(ConfigOverrideValidator.class);

    private ConfigOverrideValidator() {
    }

    static void validate(Set<ConfigProvider> providers) {
        Set<String> sharedKeys = findDuplicateEntries(providers);
        if (!sharedKeys.isEmpty()) {
            LOGGER.severe("Discovered conflicting entries: " + String.join(",", sharedKeys));
            throw new InvalidConfigurationException("Discovered conflicting configuration entries");
        }
    }

    private static Set<String> findDuplicateEntries(Set<ConfigProvider> providers) {
        return providers.stream().flatMap(p -> p.properties().keySet().stream()).map(e -> e.replace("-", "")).collect(Collectors.groupingBy(i -> i, Collectors.counting())).entrySet().stream().filter(e -> e.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toSet());
    }
}
